﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MyInteractiveSerieList.Properties;
using SerieInterface;

namespace MyInteractiveSerieList
{
    public class ListSerie : IListSerie
    {
        private string pathToSeries = Settings.Default.PathToSeries;
        private List<ISerieInfo> listOfSeries = new List<ISerieInfo>();

        public ListSerie()
        {
            foreach (string SeriePath in pathToSeries.Split(';'))
            {
                addToSerieList(SeriePath);
            }
        }

        void helping_ListUpdated(List<ISerieInfo> listOfSeries)
        {
            this.listOfSeries = listOfSeries;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        private void addToSerieList(string path)
        {
            if (string.IsNullOrEmpty(path))
                return;

            DirectoryInfo dir = new DirectoryInfo(path);
            DirectoryInfo[] subDirs = dir.GetDirectories();
            foreach (DirectoryInfo subDir in subDirs)
            {
                try { subDir.GetDirectories("S*"); }
                catch { continue; }
                if (subDir.GetDirectories("S*").Length > 0)
                {
                    ISerieInfo sI = parseSerieInfo(subDir);
                    if (sI != null && !listOfSeries.Contains(sI))
                        listOfSeries.Add(sI);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        private void removeFromSerieList(string path)
        {
            int startIndex = -1, stopIndex = 0, counter = 0;

            foreach (ISerieInfo sI in listOfSeries)
            {
                if (startIndex == -1)
                    if (string.Compare(path, sI.SerieFullPath.Remove(sI.SerieFullPath.Length - sI.SerieName.Length), true) == 0)
                        startIndex = counter;

                if (string.Compare(path, sI.SerieFullPath.Remove(sI.SerieFullPath.Length - sI.SerieName.Length), true) == 0)
                    stopIndex++;

                counter++;
            }
            if (startIndex  != -1)
                listOfSeries.RemoveRange(startIndex, stopIndex);        
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        public void removeFromSeriePath(params string[] paths)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string path in paths)
            {
                foreach (string seriePath in pathToSeries.Split(';'))
                {
                    if (string.Compare(path,seriePath,true) != 0 && !string.IsNullOrEmpty(seriePath))
                        sb.Append(seriePath + ";");
                }
                removeFromSerieList(path);
            }
            pathToSeries = sb.ToString();
            Settings.Default.PathToSeries = pathToSeries;
            Settings.Default.Save();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pathToSerie"></param>
        public void addToSeriePath(string pathToSerie)
        {
            if (Directory.Exists(pathToSerie))
            {
                foreach (string SeriePath in pathToSeries.Split(';'))
                {
                    if (string.Compare(pathToSerie, SeriePath, true) == 0)
                        return;
                }
                pathToSeries += pathToSerie + ";";
                Settings.Default.PathToSeries = pathToSeries;
                Settings.Default.Save();
                addToSerieList(pathToSerie);
            }
            //TODO: Tell user that the path didn't exist
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string getSeriePath()
        {
            return pathToSeries;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<ISerieInfo> getListOfSeries()
        {

            return listOfSeries;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serieInfo"></param>
        /// <returns></returns>
        private ISerieInfo parseSerieInfo(DirectoryInfo serieInfo)
        {
            foreach (DirectoryInfo dir in serieInfo.GetDirectories())
            {
                string temp = dir.FullName.Substring(dir.FullName.LastIndexOf('\\') + 1);
                int junk;

                if (int.TryParse(temp.Substring(1), out junk))
                {
                    ISerieInfo sI = new SerieInfo(serieInfo.FullName.Substring(serieInfo.FullName.LastIndexOf('\\')
                        + 1), serieInfo.FullName, true);
                    return sI;
                }

            }
            return null;
        }

        public void UpdateList(List<ISerieInfo> serieList)
        {
            listOfSeries = serieList;
        }
    }
}
